/* 
 * Copyright (C) 2008 Benjamin Maus < info <at> allesblinkt.com >
 *
 * This file is part of LeicasDream
 *
 * LeicasDream is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * LeicasDream is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with LeicasDream.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.allesblinkt.leicasdream;

import gestalt.Gestalt;
import gestalt.candidates.JoglGLUTBitmapFont;
import gestalt.impl.jogl.extension.quadline.JoglQuadLine;
import gestalt.impl.jogl.shape.material.JoglTexturePlugin;
import gestalt.render.AnimatorRenderer;
import gestalt.shape.material.TexturePlugin;
import gestalt.texture.Bitmaps;

import java.util.Enumeration;
import java.util.Hashtable;

import werkzeug.interpolation.InterpolateLinear;
import werkzeug.interpolation.Interpolator;

import com.sun.opengl.util.GLUT;

import data.Resource;

import mathematik.Vector2f;
import mathematik.Vector3f;

public class SputnikTagManager implements Constants {

	private Hashtable<String, SputnikTag> _myTags = new Hashtable<String, SputnikTag>(
			CAPACITY);
	private AnimatorRenderer _myRenderer;
	String _myOwnId = "0";

	private int _tagCount = 0;
	private int _activeTagCount;

	private TexturePlugin _tagTexture;
	
	public float lastServerResponseTime = 1f;

	private Interpolator _myInterpolator;
	public String searchId = "0000";

	public SputnikTagManager(AnimatorRenderer theRenderer, String ownId) {
		_myRenderer = theRenderer;
		_myOwnId = ownId;

		_myInterpolator = new Interpolator(new InterpolateLinear());
		
		_tagTexture = new JoglTexturePlugin();
		_tagTexture.load(Bitmaps.getBitmap(Resource.getStream("test.png")));
		_tagTexture.setFilterType(Gestalt.TEXTURE_FILTERTYPE_MIPMAP);
	}

	public void activateTag(String oid) {
		if (_myTags.containsKey(oid)) {
			SputnikTag tag = _myTags.get(oid);
			tag.activate();
			System.out.println("[Button pressed] " + oid);
		}

	}

	public void updateTag(String oid, Vector3f sightingPosition) {

		if (!_myTags.containsKey(oid)) {
			SputnikTag tag = new SputnikTag(oid);
			addTag(oid, tag);
			System.out.println("[Tag added] " + oid + " tagCount:" + _tagCount);

		}

		SputnikTag tag = _myTags.get(oid);
		tag.setPosition(sightingPosition);

	}

	private void addTag(String oid, SputnikTag theTag) {

		if (_myOwnId.equals(oid)) {
			theTag.isMyOwn = true;
		}

		theTag.drawable = _myRenderer.drawablefactory().plane();
		theTag.drawable.scale().set(7f, 14f, 7f);
		theTag.drawable.material().addTexture(_tagTexture);
		
		theTag.drawable.setPlaneSizeToTextureSize();
		theTag.drawable.scale().scale(0.3f);
		
		theTag.drawable.origin(Gestalt.SHAPE_ORIGIN_BOTTOM_CENTERED);
		
		theTag.drawable.material().color.set(1f, 0, 0, 0.6f);
		theTag.label = new JoglGLUTBitmapFont();
		theTag.label.color.set(1f, 1f, 1f, 1f);
		theTag.label.align = JoglGLUTBitmapFont.LEFT;
		theTag.label.font = GLUT.BITMAP_HELVETICA_10;
		theTag.label.text = String.valueOf(theTag.oid);
		
		
		
      
		/* Add to render bin */

//		_myRenderer.bin(Gestalt.BIN_3D).add(theTag.quadLine);
		_myRenderer.bin(Gestalt.BIN_3D).add(theTag.drawable);
		_myRenderer.bin(Gestalt.BIN_3D).add(theTag.label);
		_myTags.put(oid, theTag);
		_tagCount++;
	}

	public void animate() {
		_activeTagCount = 0;
		
		/* Find the direction the camera is having in relation to the zero point */
		Vector2f cameraFloorPosition = new Vector2f(_myRenderer.camera().position().x, _myRenderer.camera().position().z );
		float cameraDirection = - cameraFloorPosition.direction() + Gestalt.PI_HALF;
		
				
		Enumeration<SputnikTag> enumer = _myTags.elements();
		while (enumer.hasMoreElements()) {
			SputnikTag tag = enumer.nextElement();

			tag.drawable.rotation().y = cameraDirection ; // face the camera *roughly*
			
			float normalTime = (float) ((System.currentTimeMillis() - tag.lastSeen) / lastServerResponseTime);

			float blend = 1 - _myInterpolator.get(Math.min(normalTime, 1));

			boolean isActive;
			boolean isChosen = false;
			
			if(searchId == "0000"){
				isActive = true;
			} else {
				if(tag.oid.equalsIgnoreCase(searchId)){
					isActive = true;
					isChosen = true;
			 	} else {
			 		isActive = false;
			 		isChosen = false;
			 	}
			}
			tag.animate(blend, isActive, isChosen);
			
			if (tag.isAlive)
				_activeTagCount++;
		}

	}

	public int getActiveTagCount() {
		return _activeTagCount;
	}

	public int getSeenTagCount() {
		return _tagCount;
	}
}
